package com.decent.ejfadmin.report.service.impl;

import com.decent.ejfadmin.collectcard.dao.CollectCardRecordDao;
import com.decent.ejfadmin.common.bean.Page;
import com.decent.ejfadmin.common.bean.SupplyProduct;
import com.decent.ejfadmin.report.dao.CollectCardReportDao;
import com.decent.ejfadmin.report.entity.OperatorReportDTO;
import com.decent.ejfadmin.report.request.ReportQueryParam;
import com.decent.ejfadmin.report.service.CollectCardReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author weicx
 * @date 2018/08/01
 */
@Service
public class CollectCardReportServiceImpl implements CollectCardReportService {
    @Autowired
    private CollectCardReportDao collectCardReportDao;
    @Autowired
    private CollectCardRecordDao collectCardRecordDao;

    /**
     * 收卡地区统计
     *
     * @param reportQueryParam 查询bean
     * @param page             分页组件
     * @return list
     */
    @Override
    public Page queryAreaReport(ReportQueryParam reportQueryParam, Page page) {
        int total = collectCardReportDao.queryAreaReportCount(reportQueryParam);
        page.setTotal(total);
        if (total == 0) {
            page.setResultList(new ArrayList());
        } else {
            page.setResultList(collectCardReportDao.queryAreaReport(reportQueryParam, page));
        }
        return page;
    }

    /**
     * 收卡运营商统计
     *
     * @param pageUnit
     * @param operatorReport
     * @return
     */
    @Override
    public Page queryOperatorReport(Page pageUnit, OperatorReportDTO operatorReport) {
        Integer equityProductType = 1100;
        //查询权益产品
        if (operatorReport.getProductType() != null && operatorReport.getProductType().compareTo(equityProductType) == 0) {
            pageUnit.setTotal(1);
            pageUnit.setResultList(collectCardReportDao.queryEquityOperatorReport(pageUnit, operatorReport));
            return pageUnit;
        }
        int total = collectCardReportDao.queryOperatorReportCount(operatorReport);
        pageUnit.setTotal(total);
        if (total == 0) {
            pageUnit.setResultList(new ArrayList());
        } else {
            pageUnit.setResultList(collectCardReportDao.queryOperatorReport(pageUnit, operatorReport));
        }
        return pageUnit;
    }

    /**
     * 收卡销卡统计
     *
     * @param reportQueryParam 查询bean
     * @param page             分页组件
     * @return Page
     */
    @Override
    public Page actualSupplyCardReportPage(ReportQueryParam reportQueryParam, Page page) {
        int total = collectCardReportDao.actualSupplyCardReportCount(reportQueryParam);
        page.setTotal(total);
        if (total == 0) {
            page.setResultList(new ArrayList());
        } else {
            List<HashMap<String, Object>> resultList = collectCardReportDao.actualSupplyCardReportPage(reportQueryParam, page);
            resultList.forEach((map) -> {
                SupplyProduct product;
                String productId = "productId";
                if (map.get(productId) != null) {
                    product = collectCardRecordDao.queryProductInfoById(Long.valueOf(map.get(productId).toString()));
                } else {
                    String area = "全国";
                    product = collectCardRecordDao.queryProductInfoByFaceAreaType(new BigDecimal(map.get("face").toString()).doubleValue(), area, Integer.valueOf(map.get("productType").toString()));
                }
                if (product != null) {
                    map.put("cardOperatorName", product.getCardOperatorName());
                }
            });
            page.setResultList(resultList);
        }
        return page;
    }
}
